package com.klangner.move.behavior
{
  import com.klangner.geom.Vector2D;
  
  public class FleeBehavior implements IBehavior
  {
    private var _position:Vector2D;
    private var _range:Number = 1000;
    
    /**
    * Constructor
    * @param point flee from this point
    * @param range distance where we are safe
    */
    public function FleeBehavior(point:Vector2D, range:Number){
      _position = point;
      _range = range;
    }
    
    /**
    * flee from this position
    */
    public function set position(newPos:Vector2D):void{
      _position = newPos;
    }
    
    
    /**
    * flee when distance is less then range from flee position
    */
    public function set range(newRange:Number):void{
      _range = newRange;
    }
    
    
    /**
    * Update position.
    */
    public function updateForce(obj:ISteeredObject):void{
      var distanceVector:Vector2D = Vector2D.subtract(_position, obj.position);
      var velocity:Vector2D;
      
      if(distanceVector.length < _range){
        velocity = distanceVector.clone().normalize();
        velocity.multiply(-(_range-distanceVector.length)/_range);
        obj.setDesiredVelocity(velocity);
      }

    }

  }
}